import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';

import '../../common/index.dart';
import 'index.dart';

class ReleasePage extends GetView<ReleaseController> {
  const ReleasePage({super.key});

  // 主视图
  Widget _buildView() {
    return Stack(
      children: [
        Column(
          children: [
            SizedBox(height: 16.h),
            // 顶部导航
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Row(
                children: [
                  SizedBox(width: 10.w),
                  GestureDetector(
                    onTap: controller.onBack,
                    child: Icon(
                      Icons.arrow_back_ios,
                      color: Colors.green,
                      size: 24.sp,
                    ),
                  ),
                  Spacer(),
                  GestureDetector(
                    onTap: controller.onRelease,
                    child: Container(
                      width: 100.w,
                      height: 40.h,
                      decoration: BoxDecoration(
                        color: Color(0xFFDAFF5F),
                        borderRadius: BorderRadius.circular(20.r),
                      ),
                      child: Center(
                        child: Text(
                          'Release',
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 16.sp,
                            fontWeight: FontWeight.w900,
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(width: 10.w),
                ],
              ),
            ),
            SizedBox(height: 32.h),
            // 标题输入框
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                decoration: BoxDecoration(
                  color: Colors.white.withOpacity(0.9),
                  borderRadius: BorderRadius.circular(10.r),
                ),
                child: TextField(
                  controller: controller.title_controller,
                  decoration: InputDecoration(
                    hintText: 'Please add a title for the work',
                    hintStyle: TextStyle(
                      color: Colors.grey,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w400,
                    ),
                    border: InputBorder.none,
                  ),
                ),
              ),
            ),
            SizedBox(height: 16.h),
            // 描述输入框
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
                height: 150.h,
                decoration: BoxDecoration(
                  color: Colors.white.withOpacity(0.9),
                  borderRadius: BorderRadius.circular(10.r),
                ),
                child: TextField(
                  controller: controller.description_controller,
                  maxLines: 5,
                  decoration: InputDecoration(
                    hintText: 'Please add a description of the work',
                    hintStyle: TextStyle(
                      color: Colors.grey,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w400,
                    ),
                    border: InputBorder.none,
                  ),
                ),
              ),
            ),
            SizedBox(height: 16.h),
            // 图片区域
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: GridView.count(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                crossAxisCount: 3,
                mainAxisSpacing: 10.h,
                crossAxisSpacing: 10.w,
                childAspectRatio: 1.0,
                children: [
                  // 已选图片列表
                  ...controller.selected_images.map((image) {
                    return Stack(
                      children: [
                        Container(
                          width: 115.w,
                          height: 115.w,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20.r),
                            image: DecorationImage(
                              image: FileImage(File(image.path)),
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                        Positioned(
                          right: 2.w,
                          top: 2.h,
                          child: GestureDetector(
                            onTap: () => controller.remove_image(
                              controller.selected_images.indexOf(image),
                            ),
                            child: SvgPicture.asset(
                              AssetsImages.delete,
                              width: 27.w,
                              height: 27.h,
                            ),
                          ),
                        ),
                      ],
                    );
                  }).toList(),
                  // 添加图片按钮 (仅当选择的图片少于9张时显示)
                  if (controller.selected_images.length < 9)
                    GestureDetector(
                      onTap: controller.pick_image,
                      child: Container(
                        width: 115.w,
                        height: 115.w,
                        decoration: BoxDecoration(
                          color: Colors.white.withOpacity(0.9),
                          borderRadius: BorderRadius.circular(20.r),
                        ),
                        child: Icon(
                          Icons.camera_alt,
                          color: Colors.grey,
                          size: 32.sp,
                        ),
                      ),
                    ),
                ],
              ),
            ),
            Spacer(),
            // EULA文本
            GestureDetector(
              onTap: () {
                final Uri url = Uri.parse(
                  "https://www.freeprivacypolicy.com/live/4c5a4a8b-f353-40b9-a2f5-1d407786f884",
                );
                launchUrl(url, mode: LaunchMode.inAppWebView);
              },
              child: Text(
                'EULA',
                style: TextStyle(
                  color: Colors.white.withOpacity(0.7),
                  fontSize: 15.sp,
                  decoration: TextDecoration.underline,
                  decorationColor: Colors.white.withOpacity(0.7),
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
            SizedBox(height: 45.h),
          ],
        ),
        // 加载遮罩
        Obx(
          () => controller.is_loading.value
              ? Container(
                  width: double.infinity,
                  height: double.infinity,
                  color: Colors.black.withOpacity(0.5),
                  child: Center(
                    child: Container(
                      padding: EdgeInsets.all(20.r),
                      decoration: BoxDecoration(
                        color: Colors.black.withOpacity(0.7),
                        borderRadius: BorderRadius.circular(10.r),
                      ),
                      child: CircularProgressIndicator(
                        color: Color(0xFFCDFF5F),
                      ),
                    ),
                  ),
                )
              : SizedBox.shrink(),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ReleaseController>(
      init: ReleaseController(),
      id: "release",
      builder: (_) {
        return GestureDetector(
          // 点击空白处关闭键盘
          onTap: () => FocusScope.of(context).unfocus(),
          child: Scaffold(
            resizeToAvoidBottomInset: false,
            body: Container(
              width: double.infinity,
              height: double.infinity,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(AssetsImages.bg),
                  fit: BoxFit.cover,
                ),
              ),
              child: SafeArea(child: _buildView()),
            ),
          ),
        );
      },
    );
  }
}
